###################################################################################################
# Franchise Expansion and Legislative Representation in the Early United States ###################
# Gordon Simpson Analysis #########################################################################
# Stephen Ansolabehere, Jaclyn Kaslovsky, and Michael Olson #######################################
###################################################################################################

###################################################################################################
# description #####################################################################################
###################################################################################################

# this script produces two appendix tables using outcomes created from gordon and simpson's (2018)
# replication data.

###################################################################################################
# working directory, packages, and function #######################################################
###################################################################################################

  require("lfe")
  require("dplyr")
  require("stargazer")

  setwd("C:/Users/flyfi/Dropbox/Research/franchise_expansion/franchise_expansion_R&R/Replication Package")

###################################################################################################
# Load Data #######################################################################################
###################################################################################################
  
  dat <- read.csv("gs_data_foranalysis.csv")
  
###################################################################################################
# Analysis ########################################################################################
###################################################################################################

# Rivers Spending
  
  riv_reg <- felm(log_riv~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)

# Harbors Spending

  har_reg <- felm(log_har~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)

# Rivers and Harbors Spending
  
  rha_reg <- felm(log_rha~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)

# Canals and Roads Spending
  
  crs_reg <- felm(log_crs~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)

# Post Offices and Government Buildings Spending
  
  pbs_reg <- felm(log_pbs~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)

# Mints Spending

  maa_reg <- felm(log_maa~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)

# Forts Spending

  faa_reg <- felm(log_faa~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)

# Lighthouse Spending

  lbb_reg <- felm(log_lbb~prop_repeal
                  +pct_slave+pct_urb+pct_urb25+logpop+delegation_size
                  |as.numeric(index):factor(state)+factor(state)+factor(year)|0|state,data=dat)


  pbp_alt_sg <- stargazer(riv_reg,har_reg,rha_reg,
                          add.lines = list(c("Mean Outcome",round(mean(dat$log_riv),2),
                                             round(mean(dat$log_har),2),
                                             round(mean(dat$log_rha),2))#,
                                           ),
                          omit.stat = c("rsq", "f", "ser"),float.env="table",column.sep.width = "0cm",
                          covariate.labels=c("No Property Requirement","Percent Enslaved",
                                             "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size"#,
                                             ),
                          dep.var.caption = "ln(Spending+1)",
                          dep.var.labels = c("Rivers","Harbors","R \\& H"),
                          label="gs",notes.append = FALSE,notes.label = "",
                          report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                          no.space = TRUE,
                          notes=paste("\\parbox[t]{0.6\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(riv_reg$clustervar[[1]])),"states. 
                                     The unit of observation is the state-year. Outcome categories based on Gordon and Simpson (2018). $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                          title="Enfranchisement and Rivers and Harbors Appropriations, Gordon and Simpson (2018) Data")
  
  cat(pbp_alt_sg, sep = '\n', file = "gs_fe.tex")
  
  local_sg <- stargazer(crs_reg,pbs_reg,maa_reg,faa_reg,lbb_reg,
                        add.lines = list(c("Mean Outcome",
                                           round(mean(dat$log_crs),2),
                                           round(mean(dat$log_pbs),2),
                                           round(mean(dat$log_maa),2),
                                           round(mean(dat$log_faa),2),
                                           round(mean(dat$log_lbb),2))),
                        omit.stat = c("rsq", "f", "ser"),
                        column.sep.width = "-5pt",
                        covariate.labels=c("No Property Requirement","Percent Enslaved",
                                           "Percent Urban","Percent Urban over 25,000","ln(Population)","Delegation Size"
                                          ),
                        dep.var.caption = "ln(Appropriations+1)",
                        dep.var.labels = c("Canals \\& Roads","Public Buildings","Mints \\& Assay Offices","Military Buildings","Lighthouses"),
                        label="local",notes.append = FALSE,notes.label = "",
                        report="vc*s",star.char=c("*","**"),star.cutoffs = c(0.10,0.05),
                        no.space = TRUE,font.size="footnotesize",
                        notes=paste("\\parbox[t]{1.05\\textwidth}{\\footnotesize \\textit{Note}: Estimates are from OLS regressions with state linear trends and year fixed effects.
                       Standard errors in parentheses adjusted for clustering within",length(unique(crs_reg$clustervar[[1]])),"states. 
                                     The unit of observation is the state-year. Outcome categories based on Gordon and Simpson (2018). $^{**}p<0.05$, $^*p<0.10$.}",sep=" "),
                        title="Enfranchisement and Local Appropriations")
  
  cat(local_sg, sep = '\n', file = "local_fe.tex")
